﻿# -*- coding: utf-8 -*-
import xlrd
import xlwt
from sshclient import SSHProxy

nwb = xlwt.Workbook(encoding='utf-8')
logs = nwb.add_sheet('logs')
ips = xlrd.open_workbook('ips-run.xls')
ip = ips.sheet_by_index(0)  # 能过序号来获取
rows = ip.nrows
print(rows)
cmd = "ps -ef|grep titanagent"
# cmd = "curl -s -L 'http://10.126.115.39/agent/download?k=8244f3c7a3693e5ee58db375223f8a0870a9fe5b&group=8&protocol=0' | bash"
# for row in range(rows):
#     try:
#         hostinfo = ip.row_values(row)
#         print("row:%s" % row)
#         print("curow:%s" % row)
#         print(hostinfo[0])
#         # print(hostinfo)
#         with SSHProxy(hostinfo[0], int(22), hostinfo[1], hostinfo[2]) as ssh:
#             result = ssh.command(cmd)
#             print(result['code'])
#             logs.write(row, 0, hostinfo[0])
#             logs.write(row, 1, hostinfo[1])
#             logs.write(row, 2, result['code'])
#             logs.write(row, 3, result['result'])
#             nwb.save('logs.xls')
#     except Exception as e:
#         logs.write(row, 0, hostinfo[0])
#         logs.write(row, 4, str(e))
#         print(hostinfo[0], hostinfo[1])
#         nwb.save('logs.xls')
#         print(e)
#         continue
hosts = ip.row_values(1)
print(hosts)
print(hosts[0])
hosts = ip.col_values(1, 1, rows)  # 获取第1列 2 到19 行的数据
print(rows)
print(hosts)
ip = ipsws.
rows = ipsws.nrows
print(rows)
file1 = file.sheet_by_index(0)  # 能过序号来获取
logs = xlrdnwb.add_sheet('logs')
rows = file1.nrows  # 获取行数
hosts = file1.col_values(0, 0, rows)  # 获取第1列 2 到19 行的数据
userlist = [{'username': 'besroot', 'pwd': 'bes123!@#'}, {'username': 'besroot', 'pwd': 'P@ssw0rdCdm'},
            {'username': 'besroot', 'pwd': 'Password!'}, {'username': 'besroot', 'pwd': 'cgx123dsfKJKJ45TpQs#y23'},
            {'username': 'root', 'pwd': 'Password!'}, {'username': 'root', 'pwd': '2020P@ss'},
            {'username': 'root', 'pwd': '2019P@ss'}, {'username': 'root', 'pwd': 'P@ssw0rdCdm'}, ]

x = 0
for host in hosts:
    for user in userlist:
        try:
            with SSHProxy(host, int(22), user['username'], user['pwd']) as ssh:
                # info = ssh.command('pwd')
                logs.write(x, 0, x)
                logs.write(x, 1, host)
                logs.write(x, 2, user['username'])
                logs.write(x, 3, user['pwd'])
                # logs.write(x, 4, info['result'])
                logs.write(x, 4, "suess")
                xlrdnwb.save('logs.xls')
                x = x + 1
                print("suess:%s" % x)
                print(host, user['username'], user['pwd'])
        except Exception as e:
            print(host, user['username'], user['pwd'])
            # print("e:%s" % e)
            # print("x:%s" % x)
            # logs.write(x, 0, x)
            # logs.write(x, 1, host)
            # logs.write(x, 2, str(e))
            # logs.write(x, 3, user['username'])
            # logs.write(x, 4, user['pwd'])
            # logs.write(x, 5, str(e))
            # logs.write(x, 6, "faile")
            # xlrdnwb.save('logs.xls')
            continue
